// 云函数 addDrawRecord
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()

exports.main = async (event, context) => {
  const { nickName, regardLevel, type, date, time } = event;
  const { OPENID } = cloud.getWXContext();

  try {
    await db.collection('drawRecords').add({
      data: {
        _openid: OPENID,
        nickName,
        regardLevel,
        type,
        date,
        time,
        createTime: new Date()
      }
    });

    return {
      code: 0,
      msg: '记录添加成功'
    };
  } catch (e) {
    return {
      code: 1,
      msg: '记录添加失败',
      error: e
    };
  }
};
